package leetcode.median;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class $90_SubsetsWithDup {

    List<List<Integer>> res = new LinkedList<>();

    List<Integer> path = new ArrayList<>();

    public List<List<Integer>> subsetsWithDup(int[] nums) {
        if (nums == null || nums.length == 0) {
            return res;
        }
        Arrays.sort(nums);
        dfs(nums, 0);
        return res;
    }

    public void dfs(int[] nums, int curIndex) {
        if (curIndex > nums.length) return;
        res.add(new LinkedList<>(path));


        for (int i = curIndex; i < nums.length; i++) {
            if (i != curIndex && nums[i] == nums[i - 1]) {
                continue;
            }
            path.add(nums[i]);
            dfs(nums, i + 1);
            path.remove(path.size() - 1);
        }
    }
}
